package com.lvwenguo.service.serviceImple;

import com.lvwenguo.dao.UserDao;
import com.lvwenguo.model.User;
import com.lvwenguo.service.UserServiceInter;
import com.lvwenguo.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class UserServiceImple implements UserServiceInter {
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Connection connection;
    private JdbcUtil jdbcUtil;
    private UserDao userDao;
    public UserServiceImple(){
        connection=JdbcUtil.getJdbcUtil().getConnertion();
        userDao=new UserDao(connection);
    }

    /*
    * 查询所有
    * */
    @Override
    public List<User> select() {
        String sql="select u.id,u.user_name,u.login_account,u.tel,u.wx_account,u.id_no,u.address,r.role_name from sys_user u,sys_role r where u.role_id=r.id and u.deleted=0";
        try {
            return userDao.selectList(sql,User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
    * 查询一个
    * */
    @Override
    public List<User> selectOnt(String name) {
        String sql="select u.id,u.user_name,u.login_account,u.tel,u.wx_account,u.id_no,u.address,r.role_name from sys_user u,sys_role r where u.role_id=r.id and u.deleted=0 and u.user_name=?";
        Object[] objects={name};
        try {
            return userDao.selectList(sql,objects,User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
     * 查询一个
     * */
    @Override
    public User userData(int id) {
        String sql="select u.id,u.user_name,u.login_account,u.password,u.tel,u.wx_account,u.id_no,u.address,r.role_name from sys_user u,sys_role r where u.role_id=r.id and u.deleted=0 and u.id=?";
        Object[] objects={id};
        try {
            return userDao.selectOnt(sql,objects,User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//用于更新新用户头像
    @Override
    public int updateImgUrl(String name,String imgurl) {
        String sql="update sys_user u set u.imgurl=? where u.login_account=?";
        Object[] objects={imgurl,name};
        try {
            return userDao.update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    /*
    * 添加用户
    * */
    @Override
    public int AddUser(User user) {
        String sql="INSERT INTO sys_user(role_id,user_name,login_account,`password`,tel,wx_account,id_no,address,create_time,update_time,deleted) values(?,?,?,?,?,?,?,?,?,?,?)";
        try {
            Object[] objects={Integer.parseInt(user.getRole_name()),user.getUser_name(),user.getLogin_account(),user.getPassword(),user.getTel(),user.getWx_account(),user.getId_no(),user.getAddress(),sdf.parse(sdf.format(new Date())),sdf.parse(sdf.format(new Date())),0};
            return userDao.Add(sql,objects);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    /*
    * 更新用户信息
    * */
    @Override
    public int updateUsre(User user) {
        String sql="UPDATE sys_user set role_id=?,user_name=?,login_account=?,`password`=?,tel=?,wx_account=?,id_no=?,address=?,create_time=?,update_time=? where id=?";
        try {
            Object[] objects={Integer.parseInt(user.getRole_name()),user.getUser_name(),user.getLogin_account(),user.getPassword(),user.getTel(),user.getWx_account(),user.getId_no(),user.getAddress(),sdf.parse(sdf.format(new Date())),sdf.parse(sdf.format(new Date())),user.getId()};
            return userDao.update(sql,objects);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    /*
    * m模拟删除
    * */
    @Override
    public int deleteUser(int id) {
        String sql="UPDATE sys_user u set u.deleted=1 where u.id=?";
        Object[] objects={id};
        try {
            return userDao.update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
